<?php
/**
 * Pertyler
 * Object Browser's Row's Objects Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require_once 'main.php';

if((empty($page->ParentTable)) OR (empty($page->Row)))
	die;

$parentTable = Pertyler_Table::Select($page->ParentTable);

$row = $parentTable->SelectRow($page->Row);

$treeView = new \KoolTreeView('treeView');

$treeView->imageFolder = $iconsFolder;

$treeView->showLines = true;

foreach($row->SelectColumnArray() AS $column)
{
	if(($column->Value()))
	{
		if($column->RelatedTable())
		{
			$link = new Link('ObjectBrowser.php', 'body');
	
			$link->ParentTable = $column->RelatedTable()->id();
	
			$link->Row = $column->Value()->id();
	
			$html = '
<i >' . $column . '</i>' . $link->Html($column->Value());
	
			$treeView->Add('root',
					$page->TreeNode . $column->backgroundName(),
					$html,
					false,
					'ball_glass_redS.gif');
		}
		elseif($column->Priority() > 1)
		{
			if(($column->ColumnType() == 'integer') OR ($column->ColumnType() == 'string') OR ($column->ColumnType() == 'date') OR ($column->ColumnType() == 'eMail') OR ($column->ColumnType() == 'webSide') OR ($column->ColumnType() == 'float'))
			{
				$html = '
<i >' . $column . '</i>
' . $column->Value();
		
				$treeView->Add('root',
						$page->TreeNode . $column->backgroundName(),
						$html,
						false,
						'ball_glass_greenS.gif');
			}
			else
			{
				$html = '
<i ><u >' . $column . '</u></i>';
		
				$treeView->Add('root',
						$page->TreeNode . $column->backgroundName(),
						$html,
						false,
						'ball_glass_yellowS.gif');
			}
		}
	}
}

foreach($parentTable->SelectRelatedColumnArray() AS $column)
{
	$treeNodesLink = new Link('ObjectBrowser_relatedTablesObjects.php');

	$treeNodesLink->ParentTable = $parentTable->id();

	$treeNodesLink->ParentRow = $row->id() ;

	$treeNodesLink->Table = $column->Parent()->id();

	$treeNodesLink->Column = $column->Priority();

	$treeView->Add('root',
			$row->id() . $column->priority() . $column->Parent()->backgroundName(),
			$column->Parent() . ' (' . $column . ')',
			false,
			'triangle_yellowS.gif',
			$treeNodesLink->Href());
}

foreach($row->SelectGroupColumnArray() AS $column)
{
	$link = new Link('group.php', 'body');

	$link->Table = $parentTable->id();

	$link->Row = $row->id();

	$link->Column = $column->Priority();

	if($column->Value())
	{
		$treeNodesLink = new Link('ObjectBrowser_groupsObjects.php');

		$treeNodesLink->ParentTable = $parentTable->id();

		$treeNodesLink->Row = $row->id() ;

		$treeNodesLink->Column = $column->Priority();

		$treeView->Add('root',
				$row->id() . $column->backgroundName(),
				$link->Html($column),
				false,
				'ball_glass_blueS.gif',
				$treeNodesLink->Href());
	}
	else
		$treeView->Add('root',
				$row->id() . $column->backgroundName(),
				$link->Html($column),
				false,
				'ball_glass_blueS.gif');
}

foreach($row->SelectTableArray() AS $table)
{
	$link = new Link('table.php', 'body');

	$link->Row = $row->id();

	$link->Table = $table->id();

	if(file_exists($treeView->imageFolder . $table->Schema() . '.gif'))
		$icon = $table->Schema() . '.gif';
	else
		$icon = 'public.gif';

	$treeNodesLink = new Link('ObjectBrowser_tablesObjects.php');

	$treeNodesLink->ParentTable = $parentTable->id();

	$treeNodesLink->ParentRow = $row->id() ;

	$treeNodesLink->Table = $table->id();

	$treeView->Add('root',
			$row->id() . $table->backgroundName(),
			$link->Html($table),
			false,
			$icon,
			$treeNodesLink->Href());
}

$treeView->isSubTree = true;

$treeView->width = '250px';

echo $treeView->Render();